package com.humngyang.com.hmy.study.mywx.Util;

import com.alibaba.fastjson.JSONObject;
import java.util.Date;

public class MethodUtils {

    //创建菜单的接口
    public static final String CREATE_MENU = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    public static final String GET_ACCESS_TOCKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    public static final String APPID = "wx0b28af826e542704";
    public static final String APPSECRET = "f990efcb93e59e7173af1aca61aa0a4d";
    public static String ACCESS_TOKEN;
    public static Long EXPIRE_TIME;
    public static final String DELETE_MENU = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
    public static final String SEND_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
    public static  String data =  "{\n" +
            "           \"touser\":\"oJZPv57pWtBKyY4mnz0FlJQoymXU\",\n" +
            "           \"template_id\":\"ocItidZoQg1eBnfGWzKSyfjMhSMAy2bYG798nVSFIpY\",\n" +
            "           \"url\":\"http://https://www.jd.com\",  \n" +
            "           \"data\":{\n" +
            "                   \"first\": {\n" +
            "                       \"value\":\"恭喜你购买成功！\",\n" +
            "                       \"color\":\"#173177\"\n" +
            "                   },\n" +
            "                   \"amount\": {\n" +
            "                       \"value\":\"39.8元\",\n" +
            "                       \"color\":\"#173177\"\n" +
            "                   },\n" +
            "                   \"time\": {\n" +
            "                       \"value\":\"2014年9月22日\",\n" +
            "                       \"color\":\"#173177\"\n" +
            "                   },\n" +
            "                   \"remark\":{\n" +
            "                       \"value\":\"欢迎再次购买！\",\n" +
            "                       \"color\":\"#173177\"\n" +
            "                   }\n" +
            "           }\n" +
            "       }";
    public static final String menu = " {\n" +
            "     \"button\":[\n" +
            "     {\t\n" +
            "          \"type\":\"click\",\n" +
            "          \"name\":\"功能简介\",\n" +
            "          \"key\":\"premiere\"\n" +
            "      },\n" +
            " {\t\n" +
            "          \"type\":\"click\",\n" +
            "          \"name\":\"我的信息\",\n" +
            "          \"key\":\"myinfo\"\n" +
            "      },\n" +
            "      {\n" +
            "           \"name\":\"详细信息\",\n" +
            "           \"sub_button\":[\n" +
            "           {\t\n" +
            "               \"type\":\"view\",\n" +
            "               \"name\":\"搜索\",\n" +
            "               \"url\":\"http://www.soso.com/\"\n" +
            "            },\n" +
            " {\t\n" +
            "               \"type\":\"view\",\n" +
            "               \"name\":\"购物\",\n" +
            "               \"url\":\"https://www.jd.com\"\n" +
            "            },\n" +
            "           ]\n" +
            "       }]\n" +
            " }\n";


    /**
     * Author : hmy
     *
     * @date 2020/3/20 23:11
     * describution : 创建菜单
     **/

    public static void createmenu(String menuJson) {
        String post = HttpUtils.post(CREATE_MENU.replace("ACCESS_TOKEN", getAccessToken()), menuJson);
        System.out.println(post);
    }

    public static String getAccessToken() {
        //当前时间大于获取时间+7200s失效
        if (ACCESS_TOKEN == null || new Date().getTime() > EXPIRE_TIME) {
            String accessToken = HttpUtils.get(GET_ACCESS_TOCKEN
                    .replace("APPID", APPID)
                    .replace("APPSECRET", APPSECRET));
            JSONObject ss = JSONObject.parseObject(accessToken);
            //获取accesstoken
            ACCESS_TOKEN = ss.getString("access_token");
            //获取有效期
            Long expires_in = ss.getLong("expires_in");
            //为了解决提前失效的问题
            EXPIRE_TIME = (expires_in - 30) * 1000 + new Date().getTime();
            //设置失效时间
        }
//        System.out.println(ACCESS_TOKEN);
        return ACCESS_TOKEN;

    }

    //删除菜单
    public static void deletemenu() {
        String access_token = HttpUtils.get(DELETE_MENU.replace("ACCESS_TOKEN", getAccessToken()));
        System.out.println(access_token);
    }
    //发送模板消息
    public static void sendTemplate(String data){
        String sendTemplate = HttpUtils.post(SEND_TEMPLATE.replace("ACCESS_TOKEN", getAccessToken()), data);
        System.out.println(sendTemplate);
    }

    public static void main(String[] args) {
//        deletemenu();
//        createmenu(menu);
        sendTemplate(data);
    }

}
